package com.crazypos.common.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.StringRedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
@Order(1)
public class RedisUtil {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /***
     * 实现命令 TTL KEY,以秒未单位，返回给定key的剩余生存时间（TTL TIME TO LIVE）
     * @param key
     * @return
     */
    public long ttl(String key) {
        return redisTemplate.getExpire(key);
    }


    /***
     * 设置过期时间，单位： 秒
     * @param key
     * @param timeout
     */
    public void expire(String key, long timeout) {
        redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
    }

    /***
     * 实现命令： INCR KEY，key+delte
     * @param key
     * @param delta
     * @return
     */
    public long incr(String key, long delta) {
        return redisTemplate.opsForValue().increment(key, delta);
    }

    /***
     * 实现命令： KEYS PARTTERN,查询所有符合模式 parttern 的 key
     * @param parttern
     * @return
     */
    public Set<String> keys(String parttern) {
        return redisTemplate.keys(parttern);
    }

    /***
     * 实现命令 DEL KEY,  删除一个key
     * @param key
     */
    public void del(String key) {
        redisTemplate.delete(key);
    }

    /***
     * 实现命令： SET KEY VALUE, 设置一个key-value
     * @param key
     * @param value
     */
    public void set(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    /***
     *
     * @param key
     * @param value
     * @param timeout
     */
    public void set(String key, String value, long timeout) {
        redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
    }

    public String get(String key) {
        return (String) redisTemplate.opsForValue().get(key);
    }

    public List<String> mget(List<String> keys) {
        return redisTemplate.opsForValue().multiGet(keys);
    }

    /***
     * 批量查询，管道PIPELINE
     * @param keys
     * @return
     */
    public List<Object> batchGet(List<String> keys) {
        List<Object> result = redisTemplate.executePipelined(new RedisCallback<Object>() {

            @Override
            public Object doInRedis(RedisConnection redisConnection) throws DataAccessException {
                StringRedisConnection stringRedisConnection = (StringRedisConnection) redisConnection;
                for (String k : keys) {
                    stringRedisConnection.get(k);
                }
                return null;
            }
        });

        return result;
    }

    public void hset(String key, String filed, Object value) {
        redisTemplate.opsForHash().put(key, filed, value);
    }

    public String hget(String key, String field) {
        return (String) redisTemplate.opsForHash().get(key, field);
    }

    public void hdel(String key, Object... fields) {
        redisTemplate.opsForHash().delete(key, fields);
    }
}
